<?php
    /*    
    fwiki requires following files:
    - users.conf
    */
    define(NAV, "nav");
    define(TITLE, "Beehicular");
    include("fwiki.php");
?>
<html>
    <head>
        <title><?php echo htmlspecialchars(TITLE); ?></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <script type="text/javascript" src="jquery-1.4.4.min.js"></script>
        <script type="text/javascript" src="jquery.autocomplete-min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#create_page input").autocomplete({ 
                    serviceUrl: "list.php",
                    deferRequestBy: 0,
                    noCache: false,
                    onSelect: function(value, data) {
                        $.ajax({
                            type: "get",
                            url: "get.php",
                            data: "page=" + value,
                            success: function(response) {
                                $("#create_page textarea").val(response);
                            }
                        });
                    }
                });
            });
            
            function create_page(page) {
                $("#create_page").slideUp();
                $("#create_page").slideDown();
                $("#create_page input").val(page);
            }
            
            function create_page_impl() {
                var page = $.trim($("#create_page input").val());
                var content = $.trim($("#create_page textarea").val());
                if (page.length == 0 || content.length == 0) {
                    alert("Please fill title and content for the entry!");
                    return;
                }
                $.ajax({
                    type: "post",
                    url: "create.php",
                    data: "page=" + page + "&content=" + content,
                    success: function(response) {
                        if (response == "ERROR") {
                        
                        } else if (response == "OK") {
                            document.location.href = "?page=" + page;
                        }
                    }
                });
            }
        </script>
        <link type="text/css" href="fwiki.css" rel="stylesheet" />
    </head>
    <body>
        <div id="create_page" style="display: none;">
            <h1>Create/modify page</h1>
            <table width="100%">
                <tr>
                    <td><input type="text" maxlength="200" /></td>
                </tr>
                <tr>
                    <td>
                        <textarea></textarea>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <button onclick="create_page_impl();"><strong>Submit</strong></button> 
                        <button onclick="$('#create_page').slideUp();">Cancel</button>
                    </td>
                </tr>
            </table>
        </div>
        <div align="right">
            <h1 style="display: inline-block;">Beehicular</h1>
            <img src="beehicular.png" width="50" align="absmiddle" />
        </div>
        <div id="side">
            <div class="box"><?php fwiki_entry(NAV) ?></div>
            <div class="box">
                <ul>
                    <li><a href="javascript:;" onclick="create_page();">Create/modify page</a></li>
                </ul>
            </div>
        </div>
        <div id="content_wrapper">
            <div id="content">
                <?php 
                    if (($page = $_GET["page"]) == NULL) 
                        $page = "home";
                    fwiki_entry($page); 
                ?>
            </div>
        </div>
    </body>
</html>
